<?php
	$root = $_SERVER['DOCUMENT_ROOT'];
	include($root . "/util/session.php");
	include($root . "/util/privilege_check.php");
	checkPrivilege("admin");

	if($_SERVER["REQUEST_METHOD"] == "POST"){
		$degreeID=mysqli_real_escape_string($db,$_POST['degreeID']);
		$sql_query="select * from degrees where degreeID = '$degreeID'";
		$result=mysqli_query($db,$sql_query);
		//check if user exists
		if(mysqli_num_rows($result) == 0){
			$error="Degree doesn't exist";
		}
		else{
			$description=mysqli_real_escape_string($db,$_POST['description']);
			$degreeName=mysqli_real_escape_string($db,$_POST['degreeName']);
			$sql_query="UPDATE `degrees` SET `degreeName` = '$degreeName', `description` = '$description' where  `degrees`.`degreeID` = '$degreeID'";
			$result=mysqli_query($db,$sql_query);
			if($result){
				header("Location: /admin/admin.php?msg=Degree Modified");
			}
		}
	}
	else{
		$degreeID=mysqli_real_escape_string($db,$_GET['degreeID']);
		$query = "SELECT * FROM degrees where degreeID='$degreeID'";
		$result = mysqli_query($db,$query);
		if(mysqli_num_rows($result) == 0){
			$error = "No such degree";
			exit;
		}
		else{
			$row=mysqli_fetch_assoc($result);
		}
	}
?>
<html>
<head>
	<title>Modify a degree</title>
  	<link rel="stylesheet" type="text/css" href="adminStyle.css">
</head>
<body>
  	<?php
  	include($root . "/admin/header.php");
	?>
	<li><a href="/admin/degreeList.php">Back</a></li>
	<form action="/admin/degreeMod.php" method="post" id="degreeForm">
		<label for="degreeID">Degree to modify</label><br>
		<input type="text" id="degreeID" name="degreeID" value="<?php echo $row['degreeID']?>"><br>
		<label for="degreeName">New degree name:</label><br>
		<input type="text" id="degreeName" name="degreeName" value="<?php echo $row['degreeName']?>"><br>
		<label for="description">New description:</label><br>
		<textarea rows="4" cols="50" name="description" id="description" form="degreeForm"><?php echo $row['description']?></textarea><br>
		<input type="submit" value="Submit">
	</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php if(isset($error)){echo $error;} ?></div>

	<?php
  	include($root . "/admin/footer.php");
	?>
</body>
</html>
